package com.bingxue.edu.management.shop.controller;

import com.bingxue.edu.common.enums.OperateSourceEnum;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.shop.model.query.OrderQuery;
import com.bingxue.edu.management.shop.model.query.OrderRecentStatQuery;
import com.bingxue.edu.management.shop.model.req.OrderCreateReq;
import com.bingxue.edu.management.shop.model.req.OrderEndValidTimeUpdateReq;
import com.bingxue.edu.management.shop.model.req.OrderPayTypeUpdateReq;
import com.bingxue.edu.management.shop.model.req.OrderPriceCalculateReq;
import com.bingxue.edu.management.shop.model.resp.OrderDetailResp;
import com.bingxue.edu.management.shop.model.resp.OrderPriceDetail;
import com.bingxue.edu.management.shop.model.resp.OrderRecentStatResp;
import com.bingxue.edu.management.shop.model.resp.OrderResp;
import com.bingxue.edu.management.shop.model.resp.OrderStatResp;
import com.bingxue.edu.management.shop.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 购课订单接口
 *
 * @author panbo
 * @since 2024/08/27 14:31
 */
@Tag(name = "购课订单接口")
@RestController
@RequestMapping("/shop/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @Operation(summary = "分页查询列表")
    @ApiResource("shop:order:manage")
    @GetMapping("/page")
    public Response<Pagination<OrderResp>> page(OrderQuery query, @Validated PageQuery pageQuery) {
        return Response.success(Pagination.of(orderService.getPage(query, pageQuery)));
    }

    @Operation(summary = "订单统计数量查询")
    @ApiResource("shop:order:manage")
    @GetMapping("/stat/recent")
    public Response<OrderRecentStatResp> getRecentOrderStat(OrderRecentStatQuery query) {
        return Response.success(orderService.getOrderRecentStat(query));
    }

    @Operation(summary = "订单统计数量查询")
    @ApiResource("shop:order:manage")
    @GetMapping("/stat")
    public Response<OrderStatResp> statistic(OrderQuery query) {
        return Response.success(orderService.getOrderStat(query));
    }

    @Operation(summary = "查询详情")
    @ApiResource("shop:order:manage")
    @GetMapping("/detail/{id}")
    public Response<OrderDetailResp> detail(@PathVariable String id) {
        OrderDetailResp detail = orderService.getDetailById(id);
        return Response.success(detail);
    }

    @Operation(summary = "计算订单价格")
    @ApiResource("shop:order:manage")
    @PostMapping("/price")
    public Response<OrderPriceDetail> calculatePrice(@RequestBody @Validated OrderPriceCalculateReq req) {
        req.setOperateSource(OperateSourceEnum.BACKEND);
        return Response.success(orderService.calculatePrice(req));
    }

    @Operation(summary="创建报名订单")
    @ApiResource("shop:order:manage")
    @PostMapping("/create")
    public Response<String> create(@RequestBody @Validated OrderCreateReq req) {
        req.setOperateSource(OperateSourceEnum.BACKEND);
        return Response.success(orderService.createOrder(req));
    }

    @Operation(summary="取消报名订单")
    @ApiResource("shop:order:manage")
    @PostMapping("/cancel/{orderId}")
    public Response<Void> cancel(@PathVariable String orderId) {
        orderService.cancelOrder(orderId);
        return Response.success();
    }

    @Operation(summary="修改订单过期时间")
    @ApiResource("shop:order:manage")
    @PostMapping("/updateEndValidTime")
    public Response<Void> updateEndValidTime(@RequestBody @Validated OrderEndValidTimeUpdateReq req) {
        orderService.updateEndValidTime(req);
        return Response.success();
    }

    @Operation(summary="修改订单支付方式")
    @ApiResource("shop:order:pay-type-update")
    @PostMapping("/updatePayType")
    public Response<Void> updatePayType(@RequestBody @Validated OrderPayTypeUpdateReq req) {
        orderService.updatePayType(req);
        return Response.success();
    }

    @Operation(summary = "导出订单列表")
    @ApiResource("shop:order:export")
    @GetMapping(value = "downloadOrderList", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportOrderList(HttpServletResponse response) throws IOException {
        orderService.exportOrderList(response);
    }
}
